#include "common.h"
#include "xgame.h"
//-----------------------------------------------------------------------------
XGame::XGame(HTMLog * logger)
{
    log=logger;
    GM_STATE=MENU;  // not realized !
}
//-----------------------------------------------------------------------------
XGame::~XGame()
{
    
}
//-----------------------------------------------------------------------------
void XGame::systemInit()
{
    printf("XGame - > systemInit()\n");

    printf("Initializing SDL\n");
    
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) 
    {
        printf("Could not initialize SDL\n");
    }
    
    printf("SDL initialized\n");

    SDL_WM_SetCaption("Battle Tanks","0.01");
       
    screen=SDL_SetVideoMode(800,600,16,SDL_SWSURFACE|SDL_ANYFORMAT);
    
    // SDL_DOUBLEBUF
    
    // SDL_WM_ToggleFullScreen(screen);
    
    if (screen == NULL)
    {
        printf("Unable to set 1024x768 video\n"); 
        exit(1);
    }
}
//-----------------------------------------------------------------------------
void XGame::init()
{
    printf("XGame -> init\n");

    systemInit();

    pObjList = new XObjList(this);

    // ===
    // Временно
    // здесь считывать объекты из карты уровня и подгружать в pObjList

    // Player и его танк :)

    XTank * plTank = new XTank(this);
    plTank->setTurnSpeed(100);
    plTank->setMaxSpeed(1);
    plTank->setAccelSpeed(1);
    plTank->setMaxAccel(1);
    plTank->init(200,200);

    player = new XPlayer(this);
    player->init(plTank,200,200);

    pObjList->addObject(plTank);

    // Вражьи танки, 3 шт
    /*
    for (int i=0;i<3;i++)
    {
        XTank * tank = new XTank(screen);
        tank->init(i*300,300);
        pObjList->addObject(tank);
    }
    */
    // ===
}
//-----------------------------------------------------------------------------
void XGame::processInput()
{
    Uint8 * keys = SDL_GetKeyState(NULL);

    player->processInput();

    //if(keys[SDLK_UP])
    //{
        //player->up(); 
    //}
}
//-----------------------------------------------------------------------------
void XGame::drawScene()
{
    pObjList->drawObjects();
    player->draw();
    SDL_Flip(screen);
}
//-----------------------------------------------------------------------------
void XGame::process()
{
    // Determine delta t (ms) of game cycle
    dT = SDL_GetTicks() - oldT;
    oldT = SDL_GetTicks();
    
    switch (GM_STATE)
    {
        case MENU:
        {
                
        }
        break;
        
        case RUN:
        {
                
        }
        break;

        case PAUSE:
        {
            
        }
        break;
    }

    processInput();
    drawScene();
}
//-----------------------------------------------------------------------------
